home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / DEMON / RISCOS2 / TCP_131S.ARC / c / ftp < prev    next >
Text File  |  1994-03-06  |  6KB  |  265 lines

  1. /* Stuff common to both the FTP server and client */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "netuser.h"
  8. #include "timer.h"
  9. #include "tcp.h"
  10. #include "ftp.h"
  11. #include "session.h"
  12. #include "misc.h"
  13.  
  14. /* FTP Data channel Receive upcall handler */
  15. void ftpdr(struct tcb *tcb, int16 cnt)
  16. {
  17.   struct session *current;
  18.   register struct ftp *ftp;
  19.   struct mbuf *bp;
  20.   register char *s;
  21.   register char *t;
  22.   register int i;
  23.   char *line;
  24.  
  25.   ftp = (struct ftp *)tcb->user;
  26.   if (ftp->state != RECEIVING_STATE)
  27.   {
  28.     close_tcp(tcb);
  29.     return;
  30.   }
  31.   for (i = 0; i < nsessions; i++)
  32.   {
  33.     if(sessions[i].type == FTP && sessions[i].cb.ftp == ftp)
  34.     {
  35.       current = sessions + i;
  36.       break;
  37.     }
  38.   }
  39.   /* This will likely also generate an ACK with window rotation */
  40.   if (recv_tcp(tcb, &bp, cnt) > 0)
  41.   {
  42.     if ((line = s = malloc(len_mbuf(bp) + 1)) == NULL)
  43.     {
  44.       cwprintf(ftp->window, "Out of memory in FTP\r\n");
  45.       free_p(bp);
  46.       return;
  47.     }
  48.     while (bp != NULLBUF)
  49.     {
  50.       t = bp->data;
  51.       while (bp->cnt-- > 0)
  52.       {
  53.         if (ftp->type == ASCII_TYPE)
  54.         {
  55.           /* if (*t != '\r') */
  56.           *s++ = *t;
  57.         }
  58.         else
  59.         {
  60.           *s++ = *t;
  61.         }
  62.         t++;
  63.       }
  64.       bp = free_mbuf(bp);
  65.     }
  66.     *s = '\0';
  67.     if (ftp->fp == stdout)
  68.     {
  69.       cwputs(ftp->window, line);
  70.     }
  71.     else
  72.     {
  73.       fwrite(line, 1, s - line, ftp->fp);
  74.       if ( ftp->hash == 1 )
  75.       {
  76.         cwprintf(ftp->window, "ftp receive: %u" , ftell(ftp->fp) ) ;
  77.         cwputchar(ftp->window, '\r');
  78.       }
  79.       else
  80.       {
  81.         while (ftp->hash && (ftell(ftp->fp) - ftp->last > ftp->hash))
  82.         {
  83.           ftp->last += ftp->hash;
  84.           cwputchar(ftp->window, '#');
  85.         }
  86.       }
  87.     }
  88.     free(line);
  89.   }
  90. }
  91. /* FTP Data channel Transmit upcall handler */
  92. void ftpdt(struct tcb *tcb, int16 cnt)
  93. {
  94.   struct ftp *ftp;
  95.   struct mbuf *bp;
  96.   register char *cp;
  97.   register int c;
  98.   int eof_flag;
  99.  
  100.   ftp = (struct ftp *)tcb->user;
  101.   if(ftp->state != SENDING_FILE_STATE && ftp->state != SENDING_DATA_STATE)
  102.   {
  103.     close_tcp(tcb);
  104.     return;
  105.   }
  106.   if((bp = alloc_mbuf(cnt)) == NULLBUF)
  107.   {
  108.     /* Hard to know what to do here */
  109.     return;
  110.   }
  111.   eof_flag = 0;
  112.   if (ftp->state == SENDING_FILE_STATE)
  113.   {
  114.     if(ftp->type == IMAGE_TYPE)
  115.     {
  116.       bp->cnt = fread(bp->data,1,cnt,ftp->fp);
  117.       if(bp->cnt != cnt)
  118.         eof_flag = 1;
  119.       if ( ftp->hash == 1 )
  120.       {
  121.         cwprintf(ftp->window, "ftp send: %u" , ftell(ftp->fp) ) ;
  122.         cwputchar(ftp->window, '\r');
  123.       }
  124.       else
  125.       {
  126.         while (ftp->hash && (ftell(ftp->fp) - ftp->last > ftp->hash))
  127.         {
  128.           ftp->last += ftp->hash;
  129.           cwputchar(ftp->window, '#');
  130.         }
  131.       }
  132.     }
  133.     else
  134.     {
  135.       cp = bp->data;
  136.       while(cnt > 1)
  137.       {
  138.         if((c = getc(ftp->fp)) == EOF)
  139.         {
  140.           eof_flag=1;
  141.           break;
  142.         }
  143.         if(c == '\n')
  144.         {
  145.           *cp++ = '\r';
  146.           bp->cnt++;
  147.           cnt--;
  148.         }
  149.         *cp++ = c;
  150.         bp->cnt++;
  151.         cnt--;
  152.       }
  153.       if ( ftp->hash == 1 )
  154.       {
  155.         cwprintf(ftp->window, "ftp send: %u" , ftell(ftp->fp) ) ;
  156.         cwputchar(ftp->window, '\r');
  157.       }
  158.       else
  159.       {
  160.         while (ftp->hash && (ftell(ftp->fp) - ftp->last > ftp->hash))
  161.         {
  162.           ftp->last += ftp->hash;
  163.           cwputchar(ftp->window, '#');
  164.         }
  165.       }
  166.     }
  167.     if(bp->cnt != 0)
  168.       send_tcp(tcb,bp);
  169.     else
  170.       free_p(bp);
  171.     if(eof_flag)
  172.     {   /* EOF seen */
  173.       fclose(ftp->fp);
  174.       ftp->fp = NULLFILE;
  175.       close_tcp(tcb);
  176.     }
  177.   }
  178.   else
  179.   {        /* SENDING_DATA_STATE */
  180.     if (ftp->type == IMAGE_TYPE)
  181.     {
  182.       if ((bp->cnt = strlen(ftp->cp)) < cnt)
  183.         eof_flag = 1;
  184.       else
  185.         bp->cnt = cnt;
  186.       strncpy(bp->data, ftp->cp, bp->cnt);
  187.       bp->data[bp->cnt] = '\0';
  188.       ftp->cp += bp->cnt;
  189.     }
  190.     else
  191.     {
  192.       cp = bp->data;
  193.       while(cnt > 1)
  194.       {
  195.         if((c = *ftp->cp++) == '\0')
  196.         {
  197.           c = EOF;
  198.           eof_flag=1;
  199.           break;
  200.         }
  201.         if(c == '\n')
  202.         {
  203.           *cp++ = '\r';
  204.           bp->cnt++;
  205.           cnt--;
  206.         }
  207.         *cp++ = c;
  208.         bp->cnt++;
  209.         cnt--;
  210.       }
  211.     }
  212.     if(bp->cnt != 0)
  213.       send_tcp(tcb,bp);
  214.     else
  215.       free_p(bp);
  216.     if(eof_flag)
  217.     {   /* EOF seen */
  218.       free(ftp->p);
  219.       ftp->p = ftp->cp = NULLCHAR;
  220.       close_tcp(tcb);
  221.     }
  222.   }
  223. }
  224. /* Allocate an FTP control block */
  225. struct ftp *ftp_create(unsigned int bufsize)
  226. {
  227.   register struct ftp *ftp;
  228.  
  229.   if((ftp = (struct ftp *)calloc(1,sizeof (struct ftp))) == NULLFTP)
  230.     return NULLFTP;
  231.   if(bufsize != 0 && (ftp->buf = malloc(bufsize)) == NULLCHAR)
  232.   {
  233.     ftp_delete(ftp);
  234.     return NULLFTP;
  235.   }
  236.   ftp->state = COMMAND_STATE;
  237.   ftp->type = ASCII_TYPE; /* Default transfer type */
  238.   return ftp;
  239. }
  240. /* Free resources, delete control block */
  241. void ftp_delete(register struct ftp *ftp)
  242. {
  243.   int i;
  244.  
  245.   if(ftp->fp != NULLFILE && ftp->fp != stdout)
  246.     fclose(ftp->fp);
  247.   if(ftp->p != NULLCHAR)
  248.     free(ftp->p);
  249.   if(ftp->data != NULLTCB)
  250.     del_tcp(ftp->data);
  251.   if(ftp->username != NULLCHAR)
  252.     free(ftp->username);
  253.   for (i = 0; i < MAXPATH; i++)
  254.     if(ftp->path[i] != NULLCHAR)
  255.       free(ftp->path[i]);
  256.   if(ftp->buf != NULLCHAR)
  257.     free(ftp->buf);
  258.   if(ftp->cd != NULLCHAR)
  259.     free(ftp->cd);
  260.   if(ftp->session != NULLSESSION)
  261.     freesession(ftp->session);
  262.   free((char *)ftp);
  263. }
  264.  
  265.